
import {Button, Card, Divider, Flex, Space} from "antd";
import IndexDBWrapper from "@/store/IndexDBWrapper.ts";
import DateTimeUtil from "@/util/DateTimeUtil.ts";
import StringUtil from "@/util/StringUtil.ts";



const testAddData = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const data = {
        username: "admin",
        path: "/nav/admin",
        icon: "icon_def",
        parentId: 5002,
        children: [
            {
                path:"/nav/admin/user",
                submenu: true,
                icon: "icon_def",
            },
            {
                path:"/nav/admin/role",
                submenu: true,
                icon: "icon_def",
            }
        ]
    }
    const res = await wrapper.add(data);
    console.log("添加数据结果:", res);
    await wrapper.closeDB();
}

const testIdbUpdate = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize()
    const data = {
        id: 3,
        username: "Catalina",
        identity: "测试用户",
        gender: '女',
        userKey: StringUtil.uuid32(),
        createDate: DateTimeUtil.currentDateTimeWithMillis()
    }
    const res = await wrapper.update(data);
    console.log("更新数据结果:", res);
    await wrapper.closeDB();
}


const testDeleteDB = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    await wrapper.deleteDatabase();
    console.log("删除数据库完成");
}
const testClearStore = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    await wrapper.clear();
    console.log("清空store完成")
}

const testCreateIndex = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    const res = await wrapper.createIndex("identity", false);
    console.log("创建索引结果:",res);
    wrapper.closeDB()
}

const testDelIndex = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    // wrapper.reinitialize();
    const res = await wrapper.deleteIndex("identity_index");
    console.log("创建删除结果:",res);
    wrapper.closeDB()
}

const testShowInfo = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.showDatabaseInfo();

    console.log("数据库信息:",res?.indexNames);
    console.log("数据库信息:",res?.storeNames);
    wrapper.closeDB();
}

const testQueryPrimaryKey = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.queryByPrimaryKey(35);
    console.log("查询结果:",res);
    wrapper.closeDB();
}

const testQueryIndexOne = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.queryByIndexOne("username", "admin");
    console.log("查询结果:",res);
    wrapper.closeDB();
}

const testQueryIndexList = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.queryByIndexList("username", "admin_su");
    res?.forEach(item => {console.log("查询List结果: ", item)})
    wrapper.closeDB();
}

const testQueryAll = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.queryAll();
    res?.forEach(item => {console.log("查询All结果: ", item)})
    wrapper.closeDB();
}

const testDeleteDataPrimaryKey = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.deleteByPrimaryKey(36);
    console.log("删除结果:",res);
    await wrapper.closeDB();
}

const testDeleteDataIndex = async () => {
    const wrapper = IndexDBWrapper.getInstance();
    wrapper.reinitialize();
    const res = await wrapper.deleteByIndex("username", "admin", true);
    console.log("删除结果:",res);
    await wrapper.closeDB();
}



const TestIndexDBComponent = () => {
    return (
        <div>
            IndexPage
            <hr/>
            <Card>
                <Flex vertical={true} gap={20}>
                    <Button style={{ width: "fit-content" }} type={"primary"} onClick={testAddData}>Test add data</Button>
                    <Button style={{ width: "fit-content" }} type={"primary"} onClick={testIdbUpdate}>Test update data for IndexDB</Button>
                    {/*<Button style={{ width: "fit-content" }} type={"primary"} onClick={testClearStore}>Test Clear Store</Button>*/}
                    <Space size={20}>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testCreateIndex}>TestCreateIndex</Button>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testDelIndex}>TestDeleteIndex</Button>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testShowInfo}>TestShowInfo</Button>
                    </Space>
                    <Space size={20}>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testQueryPrimaryKey}>TestQueryPrimaryKey</Button>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testQueryIndexOne}>TestQueryIndexOne</Button>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testQueryIndexList}>TestQueryIndexList</Button>
                        <Button style={{ width: "fit-content" }} type={"primary"} onClick={testQueryAll}>TestQueryAll</Button>
                    </Space>
                    <Space size={30}>
                        <Button danger style={{ width: "fit-content" }} type={"primary"} onClick={testDeleteDataPrimaryKey}>TestDeleteDataPrimaryKey</Button>
                        <Button danger style={{ width: "fit-content" }} type={"primary"} onClick={testDeleteDataIndex}>TestDeleteDataIndex</Button>
                    </Space>
                    <Divider/>
                    <Space size={30}>
                        <Button danger style={{ width: "fit-content" }} type={"primary"} onClick={testDeleteDB}>TestDeleteDB</Button>
                        <Button danger style={{ width: "fit-content" }} type={"primary"} onClick={testClearStore}>TestClearStore</Button>
                    </Space>
                </Flex>

            </Card>

        </div>
    );
};

export default TestIndexDBComponent;